package com.ym.b2b.order.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ym.b2b.commons.conf.B2BConstants;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.order.dao.EmShoppingCartDao;
import com.ym.b2b.order.service.EmShoppingCartService;
import com.ym.b2b.order.sqlbean.EmSShoppingCart;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Controller
@RequestMapping("/shoppingCart")
public class ShoppingCartController extends BaseController {

    private Logger                logger = Logger.getLogger(getClass());

    @Autowired
    private EmShoppingCartService emShoppingCartService;

    @Autowired
    private EmShoppingCartDao     emShoppingCartDao;

    /**
     * @Title: 购物车列表
     * @Description: TODO
     * @Param @param emSShoppingCart
     */

    @RequestMapping("searchShoppingCart.s")
    @ResponseBody
    public Result searchShoppingCart(EmSShoppingCart emSShoppingCart, Page<EmSShoppingCart> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser tokenUser = getWebTokenUser(req);

            emSShoppingCart.setTokenUser(tokenUser);

            result = emShoppingCartService.searchPageShoppingCart(emSShoppingCart, page);
            result.putData("svo", emSShoppingCart);
            result.putData("info", req.getAttribute("info"));

        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * ajax加入购物车列表
     * 
     * @Title: ajaxAddShoppingCart
     */
    @RequestMapping("ajaxAddShoppingCart.s")
    @ResponseBody
    public Result ajaxAddShoppingCart(EmSShoppingCart emSShoppingCart, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "请先登陆采购商!");
                return result;
            }
            emSShoppingCart.setTokenUser(token);
            result = emShoppingCartService.addShoppingCart(emSShoppingCart);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("ajaxAddShoppingCart2.s")
    @ResponseBody
    public Result ajaxAddShoppingCart2(EmSShoppingCart emSShoppingCart, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "请先登陆采购商!");
                return result;
            }
            emSShoppingCart.setTokenUser(token);
            result = emShoppingCartService.addShoppingCart(emSShoppingCart);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * ajax加入购物车列表
     * 
     * @Title: ajaxAddShoppingCart
     */
    @RequestMapping("addShoppingCart.s")
    public ModelAndView addShoppingCart(EmSShoppingCart emSShoppingCart, HttpServletRequest req) {
        ModelAndView m = new ModelAndView();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                m.setViewName(B2BConstants.WEB_NOT_LOGIN);
                return m;
            }
            emSShoppingCart.setTokenUser(token);

            Result ret = emShoppingCartService.addShoppingCart(emSShoppingCart);
            m.addObject("ret", ret);
            m.setViewName("center/trade/shoppingcart/c_shopping_cart_list");

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            m.setViewName(B2BConstants.WEB_ERROR);
        }
        return m;
    }

    /**
     * 更新购物车信息
     * 
     * @Title: updateShoppingCart
     * @Description: TODO
     * @Param @param emSShoppingCart
     * @Param @param req
     * @Param @return
     * @Return Result
     * @Throws
     */
    @RequestMapping("updateShoppingCart.s")
    @ResponseBody
    public Result updateShoppingCart(EmSShoppingCart emSShoppingCart, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "~@^_^@~ 请先登陆采购商!");
                return result;
            }
            emSShoppingCart.setTokenUser(token);
            result = emShoppingCartService.updateShoppingCart(emSShoppingCart);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * 删除购物车信息
     * 
     * @Title: deleteShoppingCart
     * @Description: TODO
     * @Param @param emSShoppingCart
     * @Param @param req
     * @Param @return
     * @Return Result
     * @Throws
     */
    @RequestMapping("deleteShoppingCart.s")
    @ResponseBody
    public Result deleteShoppingCart(EmSShoppingCart emSShoppingCart, HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                result.setCodeResult(Result.CODE_100, "请先登陆采购商!");
                return result;
            }
            emSShoppingCart.setTokenUser(token);
            result = emShoppingCartService.deleteShoppingCart(emSShoppingCart);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("countShoppingCartNum.s")
    @ResponseBody
    public Integer countShoppingCartNum(EmSShoppingCart emSShoppingCart, HttpServletRequest req) {
        Integer a = 0;
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                return 0;
            }
            emSShoppingCart.setTokenUser(token);
            a = emShoppingCartDao.countShoppingCartNum(token.getBuyerCode());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return a;
    }
}
